$KCODE = 'u'
require 'find'

# 该版本是在jcai的auto_product_bean_pro.rb移植而来，修正了在window下无法正确删除文件的bug
# 
# 通过读取java文件来产生对应的属性名称的声明.
# 
# @author: jun tsai
# @author: Ghostbb
# @version $Revision: 2871 $
# @since 0.7.20
# 
class Auto_fix_bean_pros
    def firstCap str
      pro = ""
        pro << str[0,1].downcase
        pro << str[1,str.length]
        pro
    end
    def firstUp str
      pro = ""
        pro << str[0,1].upcase
        pro << str[1,str.length]
        pro
    end

    def generate_pros_file
        #contain the file_name as the key and the tmp_file_name as the value
        file_names_map = {}
        dir =  File.dirname(__FILE__)+'/../src/main/java/lichen/entities'
        Find.find(dir)  do |file|
          next unless file =~/\.java$/
          i=0
          pros=[]

          last_l=0
          #do with the file 1nd
          open(file) do|file_content|
              file_content.each{|line|
                i+=1
                if line =~ /private[\s]+([\w<>?]+)[\s]+([\w]+);/
                  pros << $2
                elsif line =~/\}/
                  last_l = i
                end
             }
      	  end
         p pros
          next if pros.length == 0
          puts file
          override = true
          tmp_file_name=file+'.tmp'
          tmp_file=File.open(tmp_file_name,'w')

          j=0
          start_properties = false
          end_properties = false
	
          #do with the file 2nd
          open(file) do|file_content|
              file_content.each{|line|
               j +=1
                if line =~ /\/\* bean properties begin \*\//
                  start_properties=true
                elsif start_properties && line =~ /\/\* bean properties end \*\//
                  end_properties = true 
                  line="";
                end

                if(j == last_l)
                  start_properties = false
                  tmp_file<< "    /* bean properties begin */\n"
                  pros.each{|str|
                    tmp_str=firstUp(str)
                    pro_name_var=""
                    tmp_str.scan(/[A-Z][a-z0-9]*/){|s|
                      pro_name_var+= s.upcase+"_"
                    }
                    pro_name_var=pro_name_var[0,(pro_name_var.length-1)]
                    tmp_file<< "    public static final String "+pro_name_var+"_PRO_NAME=\"#{str}\";\n"
                  }
                   tmp_file<< "    /* bean properties end */\n"    
                end
                tmp_file << line unless start_properties && !end_properties
              } 
          end
    
          tmp_file.close
          file_names_map[file]=tmp_file_name if override
          puts file_names_map
        end 
        #return the file_names_map contain all the files which we want to delete
        return file_names_map
    end
	
    #delete the file by name
    #file_names_map is a Array contain the file_name which should be delete
    def delete_file_with_filename(file_names_map)
        if file_names_map != nil
            file_names_map.keys.each{|file_name|
                puts file_name
                File.delete(file_name)
                File.rename(file_names_map[file_name],file_name)
                File.delete(file_names_map[file_name]) if File.exist?(file_names_map[file_name])
            } 
        end    
        puts 'all done! enjoy it!:)'
    end
end

bean_pros_tools = Auto_fix_bean_pros.new
file_names_map = bean_pros_tools.generate_pros_file
bean_pros_tools.delete_file_with_filename(file_names_map)